package com.cyt.work.antiepidemic.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.antiepidemic.dao.AntiepidemicAreaDao;
import com.cyt.work.antiepidemic.model.po.AntiepidemicArea;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicAreaVo;
import com.cyt.work.antiepidemic.service.AntiepidemicAreaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * e门通-省市区国标码Service层实现类
 *
 * @author rong yuan
 * @date 2020-2-12 1:40:54
 */
@Service("antiepidemicAreaService")
public class AntiepidemicAreaServiceImpl extends BaseServiceImpl<AntiepidemicArea> implements AntiepidemicAreaService {

    @Resource
    private AntiepidemicAreaDao antiepidemicAreaDao;

    @Autowired
    public void setBaseDao(AntiepidemicAreaDao antiepidemicAreaDao) {
        this.baseDao = antiepidemicAreaDao;
    }

    /**
     * 分页查询
     *
     * @param antiepidemicAreaVo 接受参数对象
     * @param pageNum            分页数
     * @param pageSize           每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:40:54
     */
    @Override
    public ReturnResult page(AntiepidemicAreaVo antiepidemicAreaVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<AntiepidemicAreaVo> list = antiepidemicAreaDao.selectByCondition(antiepidemicAreaVo);
        PageInfo<AntiepidemicAreaVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param antiepidemicAreaVo 参数接受对象
     * @param sysUser            当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:40:54
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(AntiepidemicAreaVo antiepidemicAreaVo, SysUser sysUser) {
        if (StringUtils.isBlank(antiepidemicAreaVo.getId())) {
            AntiepidemicArea antiepidemicArea = new AntiepidemicArea(sysUser);
            antiepidemicArea.setCode(antiepidemicAreaVo.getCode());
            antiepidemicArea.setName(antiepidemicAreaVo.getName());
            antiepidemicArea.setParentCode(antiepidemicAreaVo.getParentCode());
            antiepidemicAreaDao.insert(antiepidemicArea);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            AntiepidemicArea antiepidemicArea = antiepidemicAreaDao.selectByPrimaryKey(antiepidemicAreaVo.getId());
            antiepidemicArea.setCode(antiepidemicAreaVo.getCode());
            antiepidemicArea.setName(antiepidemicAreaVo.getName());
            antiepidemicArea.setParentCode(antiepidemicAreaVo.getParentCode());
            antiepidemicArea.setUpdateTime(new Date());
            antiepidemicArea.setUpdateUser(sysUser.getId());
            antiepidemicAreaDao.updateByPrimaryKey(antiepidemicArea);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param antiepidemicAreaVo 参数接受对象
     * @param sysUser            当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:40:54
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(AntiepidemicAreaVo antiepidemicAreaVo, SysUser sysUser) {
        //要修改的字段
        AntiepidemicArea antiepidemicArea = new AntiepidemicArea();
        antiepidemicArea.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        antiepidemicArea.setUpdateUser(sysUser.getId());
        antiepidemicArea.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(AntiepidemicArea.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicAreaVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicAreaVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        antiepidemicAreaDao.updateByExampleSelective(antiepidemicArea, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param antiepidemicAreaVo 参数接受对象
     * @param sysUser            当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:40:54
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(AntiepidemicAreaVo antiepidemicAreaVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(AntiepidemicArea.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicAreaVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicAreaVo.getId().split(",")));
        }
        antiepidemicAreaDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}